package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.query.impl.MenuQuery;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    IMenuService menuService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"menu:view","菜单界面"},logical= Logical.OR)
    public String view() {
        return "menu";
    }
 
    @RequestMapping("getAll")
    @ResponseBody
    public List<Menu> getAll(MenuQuery menuQuery) {
        return menuService.getAll(menuQuery);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"menu:save","保存菜单"},logical= Logical.OR)
    public JSONResult saveOrUpdate(Menu menu){
        try {
            menuService.saveOrUpdate(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"menu:delete","删除菜单"},logical= Logical.OR)
    public JSONResult delete(Long id){
        try {
            menuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    //获取所有菜单
    @RequestMapping("selectRootMenu")
    @ResponseBody
    public List<Menu> selectRootMenu(){

        return menuService.selectRootMenu();

    }

    //获取当前用户所用的菜单
    @RequestMapping("selectEmployeeMenu")
    @ResponseBody
    public List<Menu> selectEmployeeMenu(){
        return menuService.selectEmployeeMenu();
    }

}
